package com.fufan.mtsaas.modules.newtech.xjsunlimittype.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fufan.mtsaas.constant.DefaultConstant;
import com.fufan.mtsaas.modules.common.bo.request.ResultPack;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import com.fufan.mtsaas.modules.common.bo.response.ResStatus;
import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.modules.newtech.xjslimittype.bo.response.XjsLimitTypeResponse;
import com.fufan.mtsaas.modules.newtech.xjstech.bo.request.XjsTechQueryRequest;
import com.fufan.mtsaas.modules.newtech.xjstech.bo.response.XjsTechResponse;
import com.fufan.mtsaas.modules.newtech.xjsunlimittype.bo.request.XjsUnLimitQueryRequest;
import com.fufan.mtsaas.modules.newtech.xjsunlimittype.bo.request.XjsUnLimitSaveRequest;
import com.fufan.mtsaas.modules.newtech.xjsunlimittype.bo.response.XjsUnLimitResponse;
import com.fufan.mtsaas.modules.newtech.xjsunlimittype.dao.XjsUnlimitTypeDao;
import com.fufan.mtsaas.modules.newtech.xjsunlimittype.model.XjsUnlimitType;
import com.fufan.mtsaas.modules.newtech.xjsunlimittype.service.XjsUnlimitTypeService;
import com.fufan.mtsaas.tools.ExcelCreateUtil;
import com.fufan.mtsaas.tools.ExcelData;
import com.fufan.mtsaas.util.DateUtils;
import com.fufan.mtsaas.util.ParamUtil;
import com.github.pagehelper.ISelect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 非限制基础数据表(XjsUnlimitType)表服务实现类
 *
 * @author Liby
 * @since 2025-01-05 19:45:31
 */
@Service("xjsUnlimitTypeServiceImpl")
@Primary
@Slf4j
public class XjsUnlimitTypeServiceImpl extends ServiceImpl<XjsUnlimitTypeDao, XjsUnlimitType> implements XjsUnlimitTypeService {
    @Resource
    private XjsUnlimitTypeDao xjsUnlimitTypeDao;

    @Override
    public Result doQueryPage(XjsUnLimitQueryRequest map) {
        Result<XjsUnLimitResponse> result = new Result<>();
        Page<XjsUnLimitResponse> page = new Page<>();
        long total = com.github.pagehelper.PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                xjsUnlimitTypeDao.selectByPage(page, map);
            }
        });


        // 分页
        ParamUtil.setPage(map.getPageSize(), map.getCurrentPage(), page);
        //分页查询
        List<XjsUnLimitResponse> list = xjsUnlimitTypeDao.selectByPage(page,map);

        page.setRecords(list);
        page.setTotal((int) total);
        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        result.setResData(page);
        return result;
    }

    @Override
    public Result doExport(XjsUnLimitQueryRequest queryRequest, RequestPojo requestPojo, HttpServletResponse response) {

        ResultPack resultPack = new ResultPack();
        Result result = new Result();
        ExcelData excelData = new ExcelData();
        excelData.setName("非限制类列表");
        String[] title = {"医保编码","状态","信息起效日期","信息失效日期","项目编码","支付办法","项目名称","项目内涵","除外内容","计价单位","收费标准","备注","限定内容","费用类别","国家医保编码"};
        Page<XjsUnLimitResponse> page = new Page<>();

        List<XjsUnLimitResponse> list = xjsUnlimitTypeDao.selectByPage(page, queryRequest);
        List<List<Object>> rows = new ArrayList<>();

        // HashMap 转换为 List<Object>
        for (XjsUnLimitResponse item : list) {
            Map<String, Object> mapEntrys = new HashMap<>();
            mapEntrys.put("医保编码", item.getMedicalInsuranceCode());
            mapEntrys.put("状态", item.getStatus());
            mapEntrys.put("信息起效日期", item.getStartDate());
            mapEntrys.put("信息失效日期", item.getExpiryDate());
            mapEntrys.put("项目编码", item.getProjectNo());
            mapEntrys.put("支付办法", item.getPaymentMethod());
            mapEntrys.put("项目名称", item.getProjectName());
            mapEntrys.put("项目内涵", item.getProjectDesc());
            mapEntrys.put("除外内容", item.getExcludeContent());
            mapEntrys.put("计价单位", item.getPriceUnit());
            mapEntrys.put("收费标准", item.getChargeStandard());
            mapEntrys.put("备注", item.getRemark());
            mapEntrys.put("限定内容", item.getLimitedContent());
            mapEntrys.put("费用类别", item.getExpenseType());
            mapEntrys.put("国家医保编码", item.getCountryMiCode());

            List<Object> row = new ArrayList<>();
            row.addAll(mapEntrys.values());
            rows.add(row);
        }

        List<String> titles = Arrays.asList(title);
        excelData.setTitles(titles);
        excelData.setRows(rows);
        String filename = String.valueOf(System.currentTimeMillis());
        ExcelCreateUtil.exportExcel(response, "unLimitType" + filename + ".xls", excelData);

        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);

        return resultPack.doResultPack(result, "", "111111");
    }


    @Override
    public Result doSave(XjsUnLimitSaveRequest entity, RequestPojo requestPojo) {
        return null;
    }

    @Override
    public Result doUpdate(XjsUnLimitSaveRequest entity, RequestPojo requestPojo) {
        return null;
    }

    @Override
    public Result doDelete(XjsUnLimitQueryRequest map, RequestPojo requestPojo) {
        return null;
    }

}
